package com.ynet.middleground.approve.service.impl;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.approve.bean.GetImageReq;
import com.ynet.middleground.approve.dto.ProcessResourceDTO;
import com.ynet.middleground.approve.model.ProcessImageModel;
import com.ynet.middleground.approve.service.ProcessImageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 流程图资源接口服务类
 * @date 2019-10-14 14:42
 */
@Api(value = "流程图资源相关接口服务", tags = {"流程图资源相关接口服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("images")
@Service(timeout = 6000, version = "0.0.1")
public class ProcessImageServiceImpl implements ProcessImageService {

    @Autowired
    private ProcessImageModel processImageModel;

    /**
     * 根据流程定义ID获取流程图资源
     *
     * @param imageReq 流程图资源请求对象
     * @return 流程图片文件流
     */
    @POST
    @Path("definition")
    @ApiOperation(value = "根据流程定义ID获取流程图资源", notes = "根据流程定义ID，获取完整的审批流程图资源, 返回图片采用 Base64 加密字符串")
    @ApiImplicitParam(name = "requestObj", dataType = "GetImageReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> getImageByDefinitionId(GetImageReq imageReq) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(processImageModel.getImageByDefinitionId(imageReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error("ProcessImageServiceImpl.getImageByDefinitionId", "获取流程图异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "获取流程图服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 根据流程实例ID获取实时流程图资源
     *
     * @param imageReq 流程图资源请求对象
     * @return 流程图片文件流
     */
    @POST
    @ApiOperation(value = "根据流程实例ID获取实时流程图资源",
        notes = "根据流程实例ID获取实时流程图资源，并体现当前流程在哪个流程节点，返回图片采用 Base64 加密字符串供渠道使用并展示。\n<br/>"
            + "注：流程定义id和流程实例id是两个不同的id，不要以为是同一个。")
    @ApiImplicitParam(name = "requestObj", dataType = "GetImageReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> getImageByInstanceId(GetImageReq imageReq) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(processImageModel.getImageByInstanceId(imageReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error("ProcessImageServiceImpl.getImageByInstanceId", "获取实时流程图异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "获取流程图服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 根据流程定义 Key 获取实时流程图资源
     *
     * @param imageReq 流程图资源请求对象
     * @return 流程图片文件流
     */
    @POST
    @Path("definition-key")
    @ApiOperation(value = "根据程定义 Key 获取流程图资源",
        notes = "根据流程定义 Key，获取完整的审批流程图资源，返回图片采用 Base64 加密字符串供渠道使用并展示。\n"
            + "<br/>注：流程定义 Key为流程标识，不会随着流程的调整而变化。")
    @ApiImplicitParam(name = "requestObj", dataType = "GetImageReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> getImageByProcessDefinitionKey(@Valid GetImageReq imageReq) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(processImageModel.getImageByProcessDefinitionKey(imageReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error("ProcessImageServiceImpl.getImageByProcessDefinitionKey", "获取实时流程图异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "获取流程图服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 获取实时流程完整资源
     *
     * @param imageReq 流程图资源请求对象
     * @return 流程资源信息
     */
    @POST
    @Path("resource")
    @ApiOperation(value = "获取流程完整资源",
        notes = "获取完整的审批流程资源，可分别根据流程定义 ID、流程实例 ID、流程定义 Key 来查询流程资源信息<br>如果都填写，仅会使用其中 1 个条件查询，顺序是流程定义 Key、流程定义 ID、流程实例 ID<br>"
            + "如果根据流程实例 ID 查询，并且该流程未结束，会返回当前活动节点")
    @ApiImplicitParam(name = "requestObj", dataType = "GetImageReq", paramType = "body", required = true)
    @Override
    public ServiceResult<ProcessResourceDTO> getResource(@Valid GetImageReq imageReq) {
        ServiceResult<ProcessResourceDTO> result = new ServiceResult<>();
        try {
            result.setResult(processImageModel.getResource(imageReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error("ProcessImageServiceImpl.getResource", "获取实时流程完整资源异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "获取实时流程完整资源异常，请联系系统管理员");
            return result;
        }
        return result;
    }

}
